<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>订单管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {include file="public/css"/}
	<style>
	.orderdetail{background:#f7f7f8;width: 94%;box-sizing:border-box;margin:0 10px;display:flex;flex-direction:column;}
	.orderdetail *{box-sizing:border-box;}
	.address{ display:flex;align-items:center;width: 100%; padding:5px; background: #FFF;}
	.address .img{width:30px}
	.address img{width:25px; height:25px;}
	.address .info{flex:1;display:flex;flex-direction:column;}
	.address .info .t1{ font-weight:bold}

	.express { width:100%;background: #fff; padding:5px;display:flex;align-items:center;margin-top:5px;}
	.express .f1{ width:30px;font-size:20px;color:#999}
	.express .f2{display:flex;flex-direction:column;flex:auto}

	.product{width:100%; padding:5px;background: #FFF;margin-top:5px;}
	.product .content{display:flex;position:relative;width: 100%; padding:5px 0px;border-bottom: 1px #e5e5e5 dashed;position:relative}
	.product .content:last-child{ border-bottom: 0; }
	.product .content img{ width: 70px; height: 70px;}
	.product .content .detail{display:flex;flex-direction:column;margin-left:7px;flex:1}
	.product .content .detail .t1{height: 30px;line-height: 15px;color: #000;}
	.product .content .detail .t2{height: 23px;line-height: 23px;color: #999;overflow: hidden;font-size:13px;}
	.product .content .detail .t3{display:flex;height:15px;line-height:15px;color: #ff4246;}
	.product .content .detail .x1{ flex:1}
	.product .content .detail .x2{ width:50px;font-size:16px;text-align:right;margin-right:4px}
	.product .content .comment{position:absolute;top:32px;right:5px;border: 1px #ffc702 solid; border-radius:5px;background:#fff; color: #ffc702;  padding: 0 5px; height: 23px; line-height: 23px;}

	.orderinfo{ width:100%;margin-top:5px;padding: 5px;background: #FFF;}
	.orderinfo .item{display:flex;width:100%;padding:5px 0;border-bottom:1px dashed #ededed;}
	.orderinfo .item:last-child{ border-bottom: 0;}
	.orderinfo .item .t1{width:100px;}
	.orderinfo .item .t2{flex:1;text-align:right}
	.orderinfo .item .red{color:red}

	.logistics{ width: 94%;  background: #fff; padding: 0 10px;display:flex;flex-direction:column;color: #979797;}
	.logistics .on{color: #23aa5e;}
	.logistics .item{display:flex;width: 96%;  margin: 0 2%;/*border-left: 1px #dadada solid;*/padding:0 0}
	.logistics .item .f1{ width:30px;position:relative}
	/*.logistics img{width: 15px; height: 15px; position: absolute; left: -8px; top:11px;}*/
	.logistics .item .f1 img{ width: 15px; height: 100%;}

	.logistics .item .f2{display:flex;flex-direction:column;flex:auto;padding:5px 0}
	.logistics .item .f2 .t1{font-size: 15px;}
	.logistics .item .f2 .t1{font-size: 13px;}
	</style>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-card layui-col-md12">
          <div class="layui-card-header">
			<div class="layui-tab layui-tab-brief">
				<ul class="layui-tab-title">
					<li {if !input('?param.status') || input('param.status')===''}class="layui-this"{/if} onclick="location.href='{:url('index')}'">全部</li>
					<li {if input('param.status')==='0'}class="layui-this"{/if} onclick="location.href='{:url('index')}/status/0'">排队中</li>
					<li {if input('param.status')=='1'}class="layui-this"{/if} onclick="location.href='{:url('index')}/status/1'">已叫号</li>
					<li {if input('param.status')=='2'}class="layui-this"{/if} onclick="location.href='{:url('index')}/status/2'">已过号</li>
					<li {if input('param.status')=='3'}class="layui-this"{/if} onclick="location.href='{:url('index')}/status/3'">已取消</li>
				</ul>
			</div>
			{if input('param.isopen')==1}<i class="layui-icon layui-icon-close" style="font-size:18px;font-weight:bold;cursor:pointer" onclick="closeself()"></i>{/if}
          </div>
          <div class="layui-card-body" pad15>
			<div class="layui-col-md12">
				<div class="layui-col-md3">
					<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="openmax('{:url('edit')}&isopen=1')">添加</button>
					<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="datadel(0)">删除</button>
				</div>
				<div class="layui-col-md9 layui-form layui-form-search">
					<div class="layui-inline layuiadmin-input-useradmin" style="display:none">
						<label class="layui-form-label">{:t('会员')}ID</label>
						<div class="layui-input-inline">
							<input type="text" name="mid" value="{$Request.param.mid}" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-inline layuiadmin-input-useradmin">
						<label class="layui-form-label">排队号码</label>
						<div class="layui-input-inline">
							<input type="text" name="order_no" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">预订人</label>
						<div class="layui-input-inline">
							<input type="text" name="linkman" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">手机号</label>
						<div class="layui-input-inline">
							<input type="text" name="tel" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">排队时间</label>
						<div class="layui-input-inline" style="width:180px">
							<input type="text" name="create_time" id="create_time" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">状态</label>
						<div class="layui-input-inline">
							<select name="status">
								<option value="">全部</option>
								<option value="0" {if input('param.status')==='0'}selected{/if}>排队中</option>
								<option value="1" {if input('param.status')==='1'}selected{/if}>已叫号</option>
								<option value="2" {if input('param.status')==='2'}selected{/if}>已过号</option>
								<option value="3" {if input('param.status')==='3'}selected{/if}>已取消</option>
							</select>
						</div>
					</div>
					<div class="layui-inline">
						<button class="layui-btn layuiadmin-btn-replys" lay-submit="" lay-filter="LAY-app-forumreply-search">
							<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
						</button>
					</div>
				</div>
			</div>
			<div class="layui-col-md12">
				<table id="tabledata" lay-filter="tabledata"></table>
			</div>
          </div>
        </div>
    </div>
  </div>
	{include file="public/js"/}
	<script>
  	var table = layui.table;
	var datawhere = {};
	{if input('?param.status') && input('param.status')!==''}
	datawhere['status'] = "{$Request.param.status}";
	{/if}
  //数据表
  var tableIns = table.render({
    elem: '#tabledata'
    ,url: "{$Request.url}" //数据接口
    ,page: true //开启分页
		//,size:'sm'
    ,cols: [[ //表头
      {type:"checkbox"},
			{field: 'id', title: 'ID',  sort: true,width:70},
			{field: 'queue_no', title: '排队号'},
			{field: 'cname', title: '队列'},
			{field: 'linkman', title: '联系人'},
			{field: 'tel', title: '电话'},
			{field: 'renshu', title: '用餐人数'},
			{field: 'create_time', title: '取号时间',sort: true,width:160,templet:function(d){ return '<div style="color:#080">'+ d.create_time+'</div>'}},
			{field: 'expiry_time', title: '叫号时间',sort: true,width:160,templet:function(d){ 
				if(!d.call_time) return '';
				return '<div style="color:#080">'+ date('Y-m-d H:i:s',d.call_time)+'</div>'
			}},
			{field: 'expiry_time', title: '过号时间',sort: true,width:160,templet:function(d){ 
				if(!d.expiry_time) return '';
				return '<div style="color:#080">'+ date('Y-m-d H:i:s',d.expiry_time)+'</div>'
			}},
			{field: 'status', title: '状态',templet:function(d){
				var html = '';
				if(d.status==0) html+='<div style="color:#ff8758">排队中</span>';
				if(d.status==1) html+='<div style="color:#008000">已叫号</span>';
				if(d.status==2) html+='<div style="color:#bbb">过号</span>';
				if(d.status==3) html+='<div style="color:#bbb">已取消</span>';
				return html;
			},width:80},
      //{field: 'createtime', title: '下单时间',sort: true,templet:function(d){ return date('Y-m-d H:i',d.createtime)},width:150},
      {field: 'operation', title: '操作',templet:function(d){
			var html = '';
			if(d.status==0){ //排队中
				html += '<button class="table-btn" onclick="setst('+d.id+',1,\''+d.queue_no+'\')">叫号</button>';
				html += '<button class="table-btn" onclick="setst('+d.id+',2,\''+d.queue_no+'\')">过号</button>';
			}
			if(d.status==1){ //已叫号
				html += '<button class="table-btn" onclick="setst('+d.id+',1,\''+d.queue_no+'\')">重复叫号</button>';
				html += '<button class="table-btn" onclick="setst('+d.id+',2,\''+d.queue_no+'\')">过号</button>';
			}
			html += '<button class="table-btn" onclick="datadel('+d.id+')">删除</button>';
			return html;
      },width:150}
    ]]
  });
	//排序
	table.on('sort(tabledata)', function(obj){
		datawhere.field = obj.field;
		datawhere.order = obj.type;
		tableIns.reload({
			initSort: obj,
			where: datawhere
		});
	});
	//日期范围选择
	layui.laydate.render({ 
		elem: '#create_time',
		trigger: 'click',
		range: '~' //或 range: '~' 来自定义分割字符
	});
	//检索
	layui.form.on('submit(LAY-app-forumreply-search)', function(obj){
		var field = obj.field
		var olddatawhere = datawhere
		datawhere = field
		datawhere.field = olddatawhere.field
		datawhere.order = olddatawhere.order
		tableIns.reload({
			where: datawhere,
			page: {curr: 1}
		});
	})
	//删除
	function datadel(id){
		var ids = [];
		if(id==0){
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择数据');
			}
			var ids = [];
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}
		layer.confirm('确定要删除吗？删除后无法恢复！',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('del')}",{ids:ids},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	function setst(id,st,queue_no){
		var tips = '确定要操作吗?';
		if(st==1){
			var tips = '确定要叫号 '+queue_no+' 吗?';
		}
		if(st==2){
			var tips = '确定要对 '+queue_no+' 进行过号操作吗?';
		}
		layer.confirm(tips,function(){
			var index = layer.load();
			$.post("{:url('setst')}",{id:id,st:st},function(data){
				if(st == 1 && data.status==1){
					websocketsend({ type:'restaurant_queue_callno',token:'{$token}',data:{ call_id:id,aid:{$aid},bid:{$bid} } });
				}
				if(st == 2 && data.status==1){
					websocketsend({ type:'restaurant_queue_cancel',token:'{$token}',data:{ call_id:id,aid:{$aid},bid:{$bid} } });
				}
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		})
	}
	

	var websocket =null;
	function websocketsend(senddata){
		if(websocket==null || websocket.readyState!=1){
			//console.log(1)
			websocket = new WebSocket('wss://{$_SERVER['HTTP_HOST']}/wss');
			websocket.onopen = function(evt) {
				websocket.send(JSON.stringify({ type:'restaurant_queue',token:'{$token}',data:{ aid:{$aid},bid:{$bid} } }))
				if(senddata){
					websocket.send(JSON.stringify(senddata))
				}
			};
			websocket.onclose = function(evt) {
					console.log('onclose');
					console.log(evt);
			};
			websocket.onmessage = function(res) {
				console.log(res.data)
				try{
					var data = JSON.parse(res.data);
					receiveMessage(data);
				}catch(e){}
			};
			websocket.onerror = function(evt) {
					console.log('onerror');
					console.log(evt);
			};
		}else{
			if(senddata)
			websocket.send(JSON.stringify(senddata))
		}
	}

	websocketsend({ type:'restaurant_queue',token:'{$token}',data:{ aid:{$aid},bid:{$bid} } });
	function receiveMessage(data){
		console.log(data)
		if(data.type == 'restaurant_queue_add' || data.type == 'restaurant_queue_cancel' || data.type == 'restaurant_queue_callno'){
			tableIns.reload();
		}
	}
	</script>



	{include file="public/copyright"/}
</body>
</html>